Skip to content

V1.0.11p#138

Merged
ALiberalVoluntarist merged 6 commits intomasterfrom
v1.0.11p
Feb 17, 2026
Merged

V1.0.11p#138
ALiberalVoluntarist merged 6 commits intomasterfrom
v1.0.11p

Conversation

@F1r3Hydr4nt
Copy link
Copy Markdown
Collaborator

@F1r3Hydr4nt F1r3Hydr4nt commented Jan 28, 2026

Fix OP_RETURN Script Parsing and Code Quality Improvements

Description of Changes

This PR fixes a critical bug in Script._build_chunks() where scripts starting with 0x00 6a (OP_FALSE OP_RETURN) were incorrectly parsed, and includes several code quality improvements.

Bug Fix: OP_RETURN Handling

  • Issue: Scripts containing OP_RETURN (0x6a) were not properly terminating script parsing. When OP_RETURN was encountered, the parser continued to parse subsequent bytes as opcodes instead of treating them as data.
  • Fix: Implemented proper OP_RETURN handling following the TypeScript SDK pattern:
    • Added conditional block tracking (OP_IF, OP_NOTIF, OP_VERIF, OP_VERNOTIF / OP_ENDIF)
    • OP_RETURN outside conditional blocks now terminates parsing and treats all remaining bytes as data
    • OP_RETURN inside conditional blocks is treated as a normal opcode
  • Impact: Scripts like 006a0454657374 now correctly parse as 2 chunks instead of 3

Code Quality Improvements

  1. Reduced Cognitive Complexity:

    • Extracted _update_conditional_depth() helper method (reduced complexity in _build_chunks())
    • Extracted _handle_op_return() helper method
    • Extracted _read_push_data() helper method
    • Extracted _parse_opcode_token() and _parse_data_token() helpers in from_asm() method
    • All methods now meet SonarQube complexity requirements (≤15)
  2. Field Renaming:

    • Renamed self.script to self._bytes to avoid confusion with the script parameter and improve code clarity
  3. Test Improvements:

    • Added comprehensive test_op_return_chunk_parsing() test that verifies the bug fix
    • Reduced code duplication by extracting _create_spend_validator() helper function
    • Removed unused variable in test_r_puzzle()

Linked Issues / Tickets

Closes #[issue-number] - Script.chunks iterator mishandles / crashes on "safe" OP_RETURN scripts starting with 0x00 6a

Related to: #135

Testing Procedure

Unit Tests Added

  • test_op_return_chunk_parsing(): Comprehensive test covering:
    • OP_FALSE OP_RETURN with data (the bug case: 006a0454657374)
    • OP_RETURN with data (no OP_FALSE prefix)
    • OP_RETURN with no data
    • OP_FALSE OP_RETURN with no data

Test Results

pytest tests/test_scripts.py -v

Result: 12/12 tests passed

@sonarqubecloud
Copy link
Copy Markdown

@F1r3Hydr4nt
Copy link
Copy Markdown
Collaborator Author

Ok I am going to base the v2 new PR branch changes off this...

@F1r3Hydr4nt F1r3Hydr4nt marked this pull request as ready for review January 28, 2026 13:24
Copy link
Copy Markdown
Collaborator

@ALiberalVoluntarist ALiberalVoluntarist left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@ALiberalVoluntarist ALiberalVoluntarist merged commit 4c30e2e into master Feb 17, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants